#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Project: spd-sxmcc
"""
@author: lyndon
@time Created on 2018/11/21 11:03
@desc
"""

import time
import datetime
import sys
import os
import random

import logging

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver import ActionChains
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities


def commonclick(driver, element_id, sleeptime=3):
    actions = ActionChains(driver)
    actions.move_to_element(element_id)
    actions.click(element_id)
    actions.perform()
    time.sleep(sleeptime)


def login_in(driver, user, pwd):
    # open login page
    driver.get('https://login.taobao.com/member/login.jhtml')
    time.sleep(3)
    sb = driver.find_element_by_class_name("login-switch")
    commonclick(driver, sb)
    userbox = driver.find_element_by_id("TPL_username_1")
    pwdbox = driver.find_element_by_id("TPL_password_1")
    userbox.clear()
    userbox.send_keys(user)
    commonclick(driver, pwdbox)
    pwdbox.send_keys(pwd)
    print('---- 滑块拖动 ----')
    source = driver.find_element_by_xpath("//*[@id='nc_1_n1z']")
    ActionChains(driver).drag_and_drop_by_offset(source, 258, 0).perform()
    time.sleep(2)
    # action = ActionChains(driver)
    # source = driver.find_element_by_id("nc_1_n1t")  # 需要滑动的元素
    # action.click_and_hold(source).perform()  # 鼠标左键按下不放
    # action.move_by_offset(298, 0)  # 需要滑动的坐标
    # action.release().perform()  # 释放鼠标

    loadmore = driver.find_element_by_id("J_SubmitStatic")
    print('----- 提交 -----')
    commonclick(driver, loadmore)
    print('------- 提交反馈 --------')
    time.sleep(20)


if __name__ == '__main__':
    # DesiredCapabilities.PHANTOMJS['phantomjs.page.settings.loadImages'] = True
    # DesiredCapabilities.PHANTOMJS[
    #     'phantomjs.page.settings.userAgent'] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:25.0) Gecko/20100101 Firefox/25.0 "
    #
    # driver = webdriver.PhantomJS(service_args=['--ignore-ssl-errors=true'])
    # driver.set_script_timeout(30)
    # driver.set_page_load_timeout(30)
    # user = 'yz271544'
    # password = 'ShiHuiTao332324'
    # login_in(driver, user, password)

    # DesiredCapabilities.PHANTOMJS['phantomjs.page.settings.loadImages'] = True
    # DesiredCapabilities.PHANTOMJS[
    #     'phantomjs.page.settings.userAgent'] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:25.0) Gecko/20100101 Firefox/25.0 "

    driver = webdriver.Chrome()
    driver.set_script_timeout(30)
    driver.set_page_load_timeout(30)
    user = 'yz271544'
    password = 'ShiHuiTao332324'
    login_in(driver, user, password)
